package com.mounting.wechat.common.http;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.mounting.wechat.common.util.MapUtil;
import com.mounting.wechat.common.util.UnicodeUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * http发送封装
 * @author : lianghanmao
 * @date : 2022/8/18
 */
public class HttpSender {

    private SimpleHttpResponse exec(HttpRequest request) {
        try (HttpResponse response = request.execute()) {
            int code = response.getStatus();
            boolean successful = response.isOk();
            String body = response.body();
            Map<String, List<String>> headers = response.headers();
            return new SimpleHttpResponse(successful, code, headers, body, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new SimpleHttpResponse(false, 500, null, null, e.getMessage());
        }
    }

    /**
     * GET 请求
     *
     * @param url URL
     * @return 结果
     */
    public SimpleHttpResponse get(String url) {
        return this.get(url, null, false);
    }

    /**
     * GET 请求
     *
     * @param url    URL
     * @param params 参数
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public SimpleHttpResponse get(String url, Map<String, String> params, boolean encode) {
        return this.get(url, params, null, encode);
    }

    /**
     * GET 请求
     *
     * @param url    URL
     * @param params 参数
     * @return 结果
     */
    public SimpleHttpResponse get(String url, String params) {
        HttpRequest request = HttpRequest.get(url);
        if (StringUtils.isNotBlank(params)){
            request.body(params);
        }
        return this.exec(request);
    }

    /**
     * GET 请求
     *
     * @param url    URL
     * @param params 参数
     * @param header 请求头
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public SimpleHttpResponse get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
        String baseUrl = UnicodeUtil.appendIfNotContain(url, "?", "&");
        url = baseUrl + MapUtil.parseMapToString(params, encode);

        HttpRequest request = HttpRequest.get(url);
        if (header != null) {
            MapUtil.forEach(header.getHeaders(), request::header);
        }

        return exec(request);
    }

    /**
     * POST 请求
     *
     * @param url URL
     * @return 结果
     */
    public SimpleHttpResponse post(String url) {
        HttpRequest request = HttpRequest.post(url);
        return this.exec(request);
    }

    /**
     * POST 请求
     *
     * @param url  URL
     * @param data JSON 参数
     * @return 结果
     */
    public SimpleHttpResponse post(String url, String data) {
        return this.post(url, data, null);
    }

    /**
     * POST 请求
     *
     * @param url    URL
     * @param data   JSON 参数
     * @param header 请求头
     * @return 结果
     */
    public SimpleHttpResponse post(String url, String data, HttpHeader header) {
        HttpRequest request = HttpRequest.post(url);

        if (StringUtils.isNotEmpty(data)) {
            request.body(data);
        }

        if (header != null) {
            MapUtil.forEach(header.getHeaders(), request::header);
        }
        return this.exec(request);
    }

    /**
     * POST 请求
     *
     * @param url    URL
     * @param params form 参数
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public SimpleHttpResponse post(String url, Map<String, String> params, boolean encode) {
        return this.post(url, params, null, encode);
    }


    /**
     * POST 请求,默认请求头
     *
     * @param url    URL
     * @param params form 参数
     * @return 结果
     */
    public SimpleHttpResponse post(String url, Map<String, Object> params) {
        return this.post(url,params,null);
    }

    /**
     * POST 请求,包含文件
     *
     * @param url    URL
     * @param params form 参数
     * @return 结果
     */
    public SimpleHttpResponse post(String url, Map<String, Object> params,HttpHeader header) {
        HttpRequest request = HttpRequest.post(url);
        MapUtil.forEach(params, request::form);
        if (header != null) {
            MapUtil.forEach(header.getHeaders(), request::header);
        }
        return this.exec(request);
    }

    /**
     * POST 请求
     *
     * @param url    URL
     * @param params form 参数
     * @param header 请求头
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public SimpleHttpResponse post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
        HttpRequest request = HttpRequest.post(url);

        if (encode) {
            MapUtil.forEach(params, (k, v) -> request.form(k, UnicodeUtil.urlEncode(v)));
        } else {
            MapUtil.forEach(params, request::form);
        }
        if (header != null) {
            MapUtil.forEach(header.getHeaders(), request::header);
        }
        return this.exec(request);
    }

}
